package com.msb.apipassenger.service;

import com.msb.apipassenger.feign.ServicePassengerUserClient;
import com.msb.apipassenger.feign.ServiceVerificationCodeClient;
import com.msb.commons.constant.CommonStatusEnum;
import com.msb.commons.constant.IdentityConstant;
import com.msb.commons.constant.TokenConstant;
import com.msb.commons.dto.VerificationCodeDTO;
import com.msb.commons.response.NumberCodeResponse;
import com.msb.commons.response.ResponseResult;
import com.msb.commons.response.TokenResponse;
import com.msb.commons.util.JwtUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;

/**
 * @Author: chshh
 * @version: 1.0
 */
@RestController
public class VerificationCodeService {

    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;

    /**
     * 生成验证码
     * @param passengerPhone
     * @return
     */
    public ResponseResult generatorCode(String passengerPhone){

        //调用验证码服务,获取验证码
        ResponseResult<NumberCodeResponse> numberCodeResponse = serviceVerificationCodeClient.numberCode();
        int numberCode = numberCodeResponse.getData().getNumberCode();

        //存入redis  key,value,到期时间(原子操作)
        stringRedisTemplate.opsForValue().set(IdentityConstant.PASSENGER +passengerPhone, String.valueOf(numberCode),2, TimeUnit.MINUTES);

        //通过短信服务商,将对应的验证码发送到手机上,暂时不用,后期对接对应的api

        return ResponseResult.success();
    }

    /**
     * 校验验证码
     * @param passengerPhone
     * @param verificationCode
     * @return
     */
    public ResponseResult generatorCodeCheck(String passengerPhone,String verificationCode){

        //1.根据手机号读取验证码
        String codeRedis = stringRedisTemplate.opsForValue().get(IdentityConstant.PASSENGER + passengerPhone);
        System.out.println("redis中的value:"+codeRedis);
        //2.校验验证码
        if(StringUtils.isBlank(codeRedis)){ //判断是否为空,是就返回true
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        if(!verificationCode.trim().equals(codeRedis.trim())){ //trim() 方法用于删除字符串的头尾空白符，空白符包括：空格、制表符 tab、换行符等其他空白符等。
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        //3.判断原来是否有此用户,并进行对应的处理
        VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
        verificationCodeDTO.setPassengerPhone(passengerPhone);
        servicePassengerUserClient.loginOrRegister(verificationCodeDTO);
        //4.颁发令牌
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER, TokenConstant.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER, TokenConstant.REFRESH_TOKEN_TYPE);

        //5.将token存到redis中
        String accessTokenKey = TokenConstant.ACCESS_TOKEN_TYPE+passengerPhone+IdentityConstant.PASSENGER;
        //控制时间交给服务端控制后,生成token时不能设置过期时间
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);

        String refreshTokenKey = TokenConstant.REFRESH_TOKEN_TYPE+passengerPhone+IdentityConstant.PASSENGER;
        //控制时间交给服务端控制后,生成token时不能设置过期时间
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);
        //6.响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }
}
